#include "MultiFilterProxy.h"

#include <QDebug>

MultiFilterProxy::MultiFilterProxy(QObject *parent)
    : QSortFilterProxyModel(parent) {}


void MultiFilterProxy::setFilterForColumn(int column, const QString &pattern) {
    m_filters[column] = pattern;
    invalidateFilter();
}

void MultiFilterProxy::clearFilters() {
    m_filters.clear();
    invalidateFilter();
}

bool MultiFilterProxy::filterAcceptsRow(int source_row,
                                        const QModelIndex &source_parent) const {
    if (m_filters.isEmpty()) return true;

    for (auto it = m_filters.constBegin(); it != m_filters.constEnd(); ++it) {
        const QModelIndex index = sourceModel()->index(source_row, it.key(), source_parent);
        if (!index.data().toString().contains(it.value(), Qt::CaseInsensitive)) {
            return false;
        }
    }
    return true;
}


QVariant MultiFilterProxy::headerData(int section, Qt::Orientation orientation, int role) const {
    if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
        return section + 1; // 返回固定行号（从1开始）
    }
    return QSortFilterProxyModel::headerData(section, orientation, role);
}
